<?php
namespace app\service;
use think\facade\Db;

class roominfoService{
    //------------------------------多条件查询房屋信息-----------------------------------------
    
    //根据小区名称查找楼栋信息
    public function groupList($community_id){
        return Db::name('roominfo')->where('community_id',$community_id)->column('group');
    }
    //根据小区名称查找楼栋信息
    public function buildingList($community_id,$group){
        if(empty($group)){
            return Db::name('roominfo')->where('community_id',$community_id)->order('id asc')->column('building');
        }
        return Db::name('roominfo')->where('community_id',$community_id)->where('group',$group)->order('id asc')->column('building');
    }
    //根据楼栋查询单元
    public function unitList($community_id,$group,$building){
        if(empty($group)){
            return Db::name('roominfo')->where('community_id',$community_id)->where('building',$building)->order('id asc')->column('unit');
        }
        return Db::name('roominfo')->where('community_id',$community_id)->where('group',$group)->where('building',$building)->order('id asc')->column('unit');
    }
    //根据单元查询房屋
    public function roomList($community_id,$group,$building,$unit){
        if(empty($group)){
            return Db::name('roominfo')->where('community_id',$community_id)->where('building',$building)->where('unit',$unit)->order('id asc')->column('room');
        }        
        return Db::name('roominfo')->where('community_id',$community_id)->where('group',$group)->where('building',$building)->where('unit',$unit)->order('id asc')->column('room');
    }
    //查询条数
    public function roomWhereNumber($community_id,$where){
        return Db::name('roominfo')->where('community_id','in',$community_id)->where($where)->count();
    }
    //查询条数
    public function roomWhere($community_id,$where){
        return Db::name('roominfo')->where('community_id','in',$community_id)->where($where)->select();
    }
    //多条件查询
    public function roomWhereList($community_id,$page,$where){
        
        return Db::name('roominfo')->where('community_id','in',$community_id)->where($where)->order('id desc')->paginate(200, false, [
            'page'  =>  $page,
            'query' =>  $where
        ]);
        return Db::name('roominfo')
                ->alias('a') 
                ->join('community b','a.community_id=b.community_id')
                ->where('a.community_id','in',$community_id)
                ->where($where)
                ->field('a.*,b.community_name')
                ->order('id asc')
                ->paginate(100, false, [
                    'page'  =>  $page,
                    'query' =>  $where
                ]);
        
    }

    //多条件查询
    public function roomWhereList2($community_id,$page,$where){
        
        return Db::name('roominfo')->where('community_id','in',$community_id)->where($where)->order('id desc')->paginate(1000, false, [
            'page'  =>  $page,
            'query' =>  $where
        ]);
        return Db::name('roominfo')
                ->alias('a') 
                ->join('community b','a.community_id=b.community_id')
                ->where('a.community_id','in',$community_id)
                ->where($where)
                ->field('a.*,b.community_name')
                ->order('id asc')
                ->paginate(1000, false, [
                    'page'  =>  $page,
                    'query' =>  $where
                ]);
        
    }
    //根据小区ID查询名称
    public function communityName($community_id){
        return Db::name('community')->where('community_id',$community_id)->value('community_name');
    }
    //查询小区数据
    public function communityList($whe){
        return Db::name('community')->where($whe)->select();
    }
    //判断门牌号是否重复
    public function roominfoName($community_id,$address){
        return Db::name('roominfo')->where('community_id',$community_id)->where('address',$address)->find();
    }
    //添加小区信息
    public function roominfoAdd($data){
        return Db::name('roominfo')->insert($data);
    }
    //查询指定ID房屋信息
    public function roominfoIdList($id){
        return Db::name('roominfo')->where('id',$id)->find();
    }
    //修改指定ID房屋信息
    public function roominfoEdit($data){
        return Db::name('roominfo')->update($data);
    }
    //查询指定id是否存在
    public function checkRoomInfo($data){
        return Db::name('roominfo')->where('id','in',$data)->select();
    }
    //查询token
    public function roominfoToken($id){
        $community_id = Db::name('roominfo')->where('id',$id)->value('community_id');
        $property_id = Db::name('community')->where('community_id',$community_id)->value('property_id');
        return Db::name('property')->where('id',$property_id)->value('accredit');
    }
    //查询token
    public function communityToken($id){
        
        $property_id = Db::name('community')->where('community_id',$id)->value('property_id');
        return Db::name('property')->where('id',$property_id)->value('accredit');
    }
    //删除指定的数据
    public function deleteRoomInfo($data) {
            $info=Db::name('roominfo')->where('id', $data)->find();
            $result=Db::table('roominfo_del')->insert($info); 
            if($result){
                return Db::name('roominfo')->where('id', $data)->delete();
            }else{
                return false;
            }         
    }
//------------------------------批量添加房屋信息-----------------------------------------
    //通过小区名称查询对应的小区id
    public function communityNameId($communityName){
        return Db::name('community')->where('community_name', $communityName)->find();
    }
    //通过Excel插入数据
    public function roomExcelAdd($data){
        return Db::name('roominfo')->insertGetId($data);
    }
    //查询房间号是否重复
    public function roomComAddress($community_id,$address){
        return Db::name('roominfo')->where('community_id',$community_id)->where('address',$address)->find();
    }
    //通过小区ID查询对应小区名称
    public function communityIdName($commId){
        return Db::name('community')->where('community_id',$commId)->find();
    }
    //同步房屋信息至支付宝
    public function roomEntryIdInfo($data){
        return Db::name('roominfo')->where('out_room_id', 'in', $data)->select();
    }
    //更改房屋状态
    public function roomInfoStatus($data){
        return Db::name('roominfo')->where('id',$data)->setField('status','1');
    }
    public function billRoominfoName($out_room_id){
        return Db::name('roominfo')->where('out_room_id',$out_room_id)->value('owner_name');
    }
}